#include "stdafx.h"
#include "Gorilla.h"
#include "mainMenu.h"
#include <string.h>
#include <stdio.h>

using namespace Ogre;

void mainMenu::showOptions()
{
atm=380;
currentMenu=TOOM;

while(oMenuState->mState!=RES)
oMenuState=oMenuState->nextState;

std::vector<Gorilla::Caption*>::iterator myIterator;
for(myIterator = oMenuButtons.begin(); 
myIterator != oMenuButtons.end();
myIterator++)
{
	Gorilla::Caption* c=(*myIterator);
	c->colour(Ogre::ColourValue(1,1,1,0.3));
}

cOptionButton=firstOptionButton;
cOptionButton->mButton->colour(Ogre::ColourValue(1,1,1,1));
cOptionButtonA=firstOptionButtonA;
cOptionButtonA->mButton->colour(Ogre::ColourValue(1,1,1,1));

}

void mainMenu::saveCfg()
{
 FILE * pFile;
 pFile = fopen ("config.ini" , "w");
 char * cstr;
fputs ("[renderer]\n" , pFile);
fputs ("width = " , pFile);
std::string a=Ogre::StringConverter::toString(gConfig->width)+"\n";
 cstr = new char [a.size()+1];
 strcpy (cstr, a.c_str());
 fputs (cstr, pFile);
fputs ("height = " , pFile);
delete cstr;
std::string a2=Ogre::StringConverter::toString(gConfig->height)+"\n";
 cstr = new char [a2.size()+1];
 strcpy (cstr, a2.c_str());
 fputs (cstr, pFile);
fputs ("godray = " , pFile);
delete cstr;
std::string a3=Ogre::StringConverter::toString(gConfig->godRay)+"\n";
 cstr = new char [a3.size()+1];
 strcpy (cstr, a3.c_str());
 fputs (cstr, pFile);
fputs ("hdr = " , pFile);
delete cstr;
std::string a4=Ogre::StringConverter::toString(gConfig->hdr)+"\n";
 cstr = new char [a4.size()+1];
 strcpy (cstr, a4.c_str());
 fputs (cstr, pFile);
fputs ("vsync = " , pFile);
delete cstr;
std::string a5=Ogre::StringConverter::toString(gConfig->vsync)+"\n";
 cstr = new char [a5.size()+1];
 strcpy (cstr, a5.c_str());
 fputs (cstr, pFile);
 delete cstr;
fclose (pFile);
}
void mainMenu::closeOptions()
{
atm=380;
currentMenu=FROM;
//oMenuState=RES;

std::vector<Gorilla::Caption*>::iterator myIterator;

for(myIterator = oMenuButtons.begin(); 
myIterator != oMenuButtons.end();
myIterator++)
{
	Gorilla::Caption* c=(*myIterator);
	c->colour(Ogre::ColourValue(1,1,1,0.3));
}

}

int mainMenu::pressedKey(const OIS::KeyEvent &arg)
{

	if(currentMenu==MAINM && !moving){
		switch (arg.key)
		{
			case OIS::KC_LEFT: 
				if(!moving)
				{
				menuMovingDir=1;
				moving=true;
				mMenuState=mMenuState->prevState;
				}
			    break;
		    case OIS::KC_RIGHT: 
			   	if(!moving)
				{
			    menuMovingDir=-1;
			    moving=true;
		    	mMenuState=mMenuState->nextState;
				}
			    break;

		    case OIS::KC_RETURN: 
				if(mMenuState->mState==QUIT)
				{
				saveCfg();
				return 1;
				}
				if(mMenuState->mState==OPTIONS)
				{
				showOptions();
				}
			    break;
           default:
              break;
		}
	}
else
if(currentMenu==OPTIONSM)
{
switch (arg.key)
		{
		   case OIS::KC_UP: 
			   oMenuState=oMenuState->prevState;
			   cOptionButton->mButton->colour(Ogre::ColourValue(1,1,1,0.3));
			   cOptionButtonA->mButton->colour(Ogre::ColourValue(1,1,1,0.3));
			   cOptionButton=cOptionButton->prevState;
			   cOptionButtonA=cOptionButtonA->prevState;
			   cOptionButton->mButton->colour(Ogre::ColourValue(1,1,1,1));
			   cOptionButtonA->mButton->colour(Ogre::ColourValue(1,1,1,1));
			   break;
		   case OIS::KC_DOWN: 
			   oMenuState=oMenuState->nextState;
			   cOptionButton->mButton->colour(Ogre::ColourValue(1,1,1,0.3));
			   cOptionButtonA->mButton->colour(Ogre::ColourValue(1,1,1,0.3));
			   cOptionButton=cOptionButton->nextState;
			   cOptionButtonA=cOptionButtonA->nextState;
			   cOptionButton->mButton->colour(Ogre::ColourValue(1,1,1,1));
			   cOptionButtonA->mButton->colour(Ogre::ColourValue(1,1,1,1));
			   break;
		   case OIS::KC_LEFT: 
			   if(oMenuState->mState==RES)
			   {resolutionsLoop=resolutionsLoop->prevRes; cOptionButtonA->mButton->text(*resolutionsLoop->res);}
			   if(oMenuState->mState==RAY)
			   {if(gConfig->godRay) cOptionButtonA->mButton->text("Off"); else cOptionButtonA->mButton->text("On");
			   gConfig->godRay=!gConfig->godRay;}
			   if(oMenuState->mState==HDR)
			   {if(gConfig->hdr) cOptionButtonA->mButton->text("Off"); else cOptionButtonA->mButton->text("On");
			   gConfig->hdr=!gConfig->hdr;}
			   if(oMenuState->mState==VSYNC)
			   {if(gConfig->vsync) cOptionButtonA->mButton->text("Off"); else cOptionButtonA->mButton->text("On");
			   gConfig->vsync=!gConfig->vsync;}
			   break;
		   case OIS::KC_RIGHT: 
			   if(oMenuState->mState==RES)
			   {resolutionsLoop=resolutionsLoop->nextRes; cOptionButtonA->mButton->text(*resolutionsLoop->res);}
			   if(oMenuState->mState==RAY)
			   {if(gConfig->godRay) cOptionButtonA->mButton->text("Off"); else cOptionButtonA->mButton->text("On");
			   gConfig->godRay=!gConfig->godRay;}
			   if(oMenuState->mState==HDR)
			   {if(gConfig->hdr) cOptionButtonA->mButton->text("Off"); else cOptionButtonA->mButton->text("On");
			   gConfig->hdr=!gConfig->hdr;}
			   if(oMenuState->mState==VSYNC)
			   {if(gConfig->vsync) cOptionButtonA->mButton->text("Off"); else cOptionButtonA->mButton->text("On");
			   gConfig->vsync=!gConfig->vsync;}
			   break;
		   case OIS::KC_RETURN: 
			   break;
		   case OIS::KC_BACK: 
			   // Ogre::LogManager::getSingleton().getDefaultLog()->logMessage("sfs",Ogre::LML_NORMAL);
			    closeOptions();
			   break;

           default:
              break;
		}
}

return 0;
}

mainMenu::mainMenu(Ogre::SceneManager *mSceneM, Ogre::Camera* mCam,Ogre::RenderWindow* mWin,Ogre::RenderSystem* rs)
{

gConfig=new GameConfig();
Ogre::ConfigFile cfgFile;
cfgFile.loadDirect("config.ini");
std::string wString = cfgFile.getSetting("width", "renderer");
std::string hString = cfgFile.getSetting("height", "renderer");
std::string grString = cfgFile.getSetting("godray", "renderer");
std::string hdrString = cfgFile.getSetting("hdr", "renderer");
std::string vsString = cfgFile.getSetting("vsync", "renderer");
gConfig->width=Ogre::StringConverter::parseInt(wString);
gConfig->height=Ogre::StringConverter::parseInt(hString);
gConfig->godRay=Ogre::StringConverter::parseBool(grString);
gConfig->hdr=Ogre::StringConverter::parseBool(hdrString);
gConfig->vsync=Ogre::StringConverter::parseBool(vsString);

Ogre::ConfigOptionMap& CurrentRendererOptions = rs->getConfigOptions();
Ogre::ConfigOptionMap::iterator configItr = CurrentRendererOptions.begin();
while( configItr != CurrentRendererOptions.end() ) 
{
  if( (configItr)->first == "Video Mode" )
  {
     // Store Available Resolutions
	  mFoundResolutions = ((configItr)->second.possibleValues);
  }
  configItr++;
}


int i=mFoundResolutions.size();
std::string currentR=Ogre::StringConverter::toString(gConfig->width)+"x"+Ogre::StringConverter::toString(gConfig->height);

resLoop* trLoop=resolutionsLoop= new resLoop();
resLoop* trLoopC=resolutionsLoop;
std::string s=mFoundResolutions.at(i/2);
char * cstr;
cstr = new char [s.size()+1];
strcpy (cstr, s.c_str());
std::string s1=strtok(cstr," ");
strtok(NULL," ");
std::string s2=strtok(NULL," ");
std::string res=s1+"x"+s2;
resolutionsLoop->res=new std::string(res);

for(int o=(i/2)+1;o<i;o++)
{
	std::string s=mFoundResolutions.at(o);
	char * cstr;
	cstr = new char [s.size()+1];
	strcpy (cstr, s.c_str());

	std::string s1=strtok(cstr," ");
	strtok(NULL," ");
	std::string s2=strtok(NULL," ");
	std::string res=s1+"x"+s2;
	resLoop* trLoopN= new resLoop();
	trLoopN->res=new std::string(res);
	if(Ogre::StringUtil::match(currentR,res)) trLoopC=trLoopN;
	trLoopN->prevRes=trLoop;
	trLoop->nextRes=trLoopN;
	trLoop=trLoopN;
}
resolutionsLoop->prevRes=trLoop;
trLoop->nextRes=resolutionsLoop;
resolutionsLoop=trLoopC;

for(int o=(i/2);o<i;o++)
{
Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(*resolutionsLoop->res,Ogre::LML_NORMAL);
resolutionsLoop=resolutionsLoop->nextRes;
}



mMenuState= new mMenuLoop();
mMenuLoop* mMenuState0= new mMenuLoop();
mMenuLoop* mMenuState2= new mMenuLoop();
mMenuState->mState=START;
mMenuState->nextState=mMenuState2;
mMenuState->prevState=mMenuState0;
mMenuState0->mState=QUIT;
mMenuState0->nextState=mMenuState;
mMenuState0->prevState=mMenuState2;
mMenuState2->mState=OPTIONS;
mMenuState2->nextState=mMenuState0;
mMenuState2->prevState=mMenuState;

oMenuState= new oMenuLoop();
oMenuLoop* oMenuState2= new oMenuLoop();
oMenuLoop* oMenuState3= new oMenuLoop();
oMenuLoop* oMenuState4= new oMenuLoop();
oMenuState->mState=RES;
oMenuState->nextState=oMenuState2;
oMenuState->prevState=oMenuState4;
oMenuState2->mState=RAY;
oMenuState2->nextState=oMenuState3;
oMenuState2->prevState=oMenuState;
oMenuState3->mState=HDR;
oMenuState3->nextState=oMenuState4;
oMenuState3->prevState=oMenuState2;
oMenuState4->mState=VSYNC;
oMenuState4->nextState=oMenuState;
oMenuState4->prevState=oMenuState3;

currentMenu=MAINM;

moving=false;
atm=0;
menuMovingDir=1;
mSceneMgr=mSceneM;
mCamera=mCam;
mWindow=mWin;
mSilverback = new Gorilla::Silverback();
mSilverback->loadAtlas("dejavu");
mScreen = mSilverback->createScreen(mCamera->getViewport(), "dejavu");
mScreen->setOrientation(Ogre::OrientationMode::OR_DEGREE_0);
Ogre::Real vpW = mScreen->getWidth(), vpH = mScreen->getHeight();

mLayer = mScreen->createLayer(0);

mouseLayer=mScreen->createLayer(1);
mousePointer = mouseLayer->createRectangle(0,0,15,22);
mousePointer->background_image("mousepointer");
mousePointer->no_background();
fpsCaption = mouseLayer->createCaption(48, 50, 5, "007");
fpsCaption->size(1500,50);
fpsCaption->colour(Ogre::ColourValue(0,255,0));
fpsCaption->align(Gorilla::TextAlign_Right);

debugCaption = mouseLayer->createCaption(48, 50, 5, "007");
debugCaption->size(1500,50);
debugCaption->colour(Ogre::ColourValue(0,0,255));
debugCaption->align(Gorilla::TextAlign_Left);

mousePointer->yes_background(1);
}

void mainMenu::setMainMenu()
{
mLayer->show();
Gorilla::Caption* caption = mLayer->createCaption(48, 690, 900 , "Start");
caption->size(300,50);
caption->colour(Ogre::ColourValue(1,1,1,1));
caption->align(Gorilla::TextAlign_Centre);
mMenuButtons.push_back(caption);
caption = mLayer->createCaption(48, 1339.5, 900 , "Options");
caption->size(300,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Centre);
mMenuButtons.push_back(caption);
caption = mLayer->createCaption(48, 40.5, 900 , "Quit");
caption->size(300,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Centre);
mMenuButtons.push_back(caption);

cOptionButton=firstOptionButton=new buttonLoop();
buttonLoop* cOptionButton2=new buttonLoop();
buttonLoop* cOptionButton3=new buttonLoop();
buttonLoop* cOptionButton4=new buttonLoop();
cOptionButton->prevState=cOptionButton4;
cOptionButton->nextState=cOptionButton2;
cOptionButton2->prevState=cOptionButton;
cOptionButton2->nextState=cOptionButton3;
cOptionButton3->prevState=cOptionButton2;
cOptionButton3->nextState=cOptionButton4;
cOptionButton4->prevState=cOptionButton3;
cOptionButton4->nextState=cOptionButton;
caption = mLayer->createCaption(48, 400, 1060 , "Option 1");
caption->size(300,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Left);
oMenuButtons.push_back(caption);
cOptionButton->mButton=caption;
caption = mLayer->createCaption(48, 400, 1150 , "Option 2");
caption->size(300,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Left);
oMenuButtons.push_back(caption);
cOptionButton2->mButton=caption;
caption = mLayer->createCaption(48, 400, 1240 , "Option 3");
caption->size(300,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Left);
oMenuButtons.push_back(caption);
cOptionButton3->mButton=caption;
caption = mLayer->createCaption(48, 400, 1330 , "Option 4");
caption->size(300,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Left);
oMenuButtons.push_back(caption);
cOptionButton4->mButton=caption;

cOptionButtonA=firstOptionButtonA=new buttonLoop();
buttonLoop* cOptionButtonA2=new buttonLoop();
buttonLoop* cOptionButtonA3=new buttonLoop();
buttonLoop* cOptionButtonA4=new buttonLoop();
cOptionButtonA->prevState=cOptionButtonA4;
cOptionButtonA->nextState=cOptionButtonA2;
cOptionButtonA2->prevState=cOptionButtonA;
cOptionButtonA2->nextState=cOptionButtonA3;
cOptionButtonA3->prevState=cOptionButtonA2;
cOptionButtonA3->nextState=cOptionButtonA4;
cOptionButtonA4->prevState=cOptionButtonA3;
cOptionButtonA4->nextState=cOptionButtonA;

std::string resolutionStr=Ogre::StringConverter::toString(gConfig->width)+"x"+Ogre::StringConverter::toString(gConfig->height);
caption = mLayer->createCaption(48, 700, 1060 , resolutionStr);
caption->size(350,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Left);
oMenuButtons.push_back(caption);
cOptionButtonA->mButton=caption;
if(gConfig->godRay) 
caption = mLayer->createCaption(48, 700, 1150 , "On"); else 
caption = mLayer->createCaption(48, 700, 1150 , "Off");
caption->size(350,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Left);
oMenuButtons.push_back(caption);
cOptionButtonA2->mButton=caption;
if(gConfig->hdr) 
caption = mLayer->createCaption(48, 700, 1240 , "On"); else
caption = mLayer->createCaption(48, 700, 1240 , "Off");
caption->size(350,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Left);
oMenuButtons.push_back(caption);
cOptionButtonA3->mButton=caption;
if(gConfig->vsync) 
caption = mLayer->createCaption(48, 700, 1330 , "On"); else
caption = mLayer->createCaption(48, 700, 1330 , "Off");
caption->size(350,50);
caption->colour(Ogre::ColourValue(1,1,1,0.3));
caption->align(Gorilla::TextAlign_Left);
oMenuButtons.push_back(caption);
cOptionButtonA4->mButton=caption;
}

void mainMenu::mouseMoveUpdate(int x,int y)
{
	Vector2 pos=mousePointer->position();
	mousePointer->position(Math::Clamp<int>(pos.x+x,0,1680-15),Math::Clamp<int>(pos.y+y,0,1050-22));
}

int mainMenu::mainMenuPressed()
	{
		Vector2 mPos=mousePointer->position();

		std::vector<Gorilla::Caption*>::iterator myIterator;
		for(myIterator = mMenuButtons.begin(); 
        myIterator != mMenuButtons.end();
        myIterator++)
		{
			Gorilla::Caption* c=(*myIterator);
			if(c->intersects(mPos))
			{
				if(Ogre::StringUtil::match(c->text(),"Quit"))
				{
					return 3;
				}
				if(Ogre::StringUtil::match(c->text(),"Start"))
				{
					mMenuButtons.clear();
					mLayer->hide();
				    mLayer->destroyAllCaptions();
					mousePointer->no_background();
					return 1;
				}
			}
		}

		return 0;
	}

void mainMenu::updateFPS(Ogre::Real value)
{
fpsCaption->text(Ogre::StringConverter::toString((int)mWindow->getStatistics().lastFPS));
debugCaption->text(Ogre::StringConverter::toString(value));
}

void mainMenu::updateMenu(Ogre::Real evt,bool rebuildMesh)
{
	fpsCaption->text(Ogre::StringConverter::toString((int)mWindow->getStatistics().lastFPS));
	debugCaption->text("re:"+Ogre::StringConverter::toString(rebuildMesh));

	if(currentMenu==TOOM)
	{
	Real rotSpeed=evt*1100;
	atm=atm-rotSpeed;
		if(atm<=0)
		{
		currentMenu=OPTIONSM;
		rotSpeed=rotSpeed+atm;
		atm=0; 
		}

		std::vector<Gorilla::Caption*>::iterator myIterator;

			for(myIterator = mMenuButtons.begin(); 
			myIterator != mMenuButtons.end();
			myIterator++)
			{
				Gorilla::Caption* c=(*myIterator);
				c->top(c->top()-rotSpeed);	
			}

			for(myIterator = oMenuButtons.begin(); 
			myIterator != oMenuButtons.end();
			myIterator++)
			{
				Gorilla::Caption* c=(*myIterator);
				c->top(c->top()-rotSpeed);	
			}

	}
	else
	if(currentMenu==FROM)
	{
	Real rotSpeed=evt*1100;
	atm=atm-rotSpeed;
		if(atm<=0)
		{
		currentMenu=MAINM;
		rotSpeed=rotSpeed+atm;
		atm=0; 
		}

		std::vector<Gorilla::Caption*>::iterator myIterator;

			for(myIterator = mMenuButtons.begin(); 
			myIterator != mMenuButtons.end();
			myIterator++)
			{
				Gorilla::Caption* c=(*myIterator);
				c->top(c->top()+rotSpeed);	
			}

			for(myIterator = oMenuButtons.begin(); 
			myIterator != oMenuButtons.end();
			myIterator++)
			{
				Gorilla::Caption* c=(*myIterator);
				c->top(c->top()+rotSpeed);	
			}

	}
	else
	if(moving)
		{
		Real rotSpeed=evt*1400;
		atm=atm+rotSpeed;
		if(atm>=666)
		{
		moving=false;
		rotSpeed=rotSpeed-(atm-666);
		atm=0; 
		}

		std::vector<Gorilla::Caption*>::iterator myIterator;
			for(myIterator = mMenuButtons.begin(); 
			myIterator != mMenuButtons.end();
			myIterator++)
			{
				//1680+2*309, roz 766
				Gorilla::Caption* c=(*myIterator);
				c->left(c->left()+rotSpeed*menuMovingDir);
				
				if(c->left()>=1689 && menuMovingDir==1)
				{
					c->left(-309+(c->left()-1689));
					
				}
				if(c->left()<=-309  && menuMovingDir==-1)
					c->left(1689-(-309-c->left()));

				Real alpha=1-Math::Abs((c->left()-690)/999);
				c->colour(Ogre::ColourValue(1,1,1,alpha));
		}
}

}